package com.wwtech.epp.ui.activity.work;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.CommentContent;
import com.wwtech.epp.db.CommentContentDao;
import com.wwtech.epp.db.DBManager;
import com.wwtech.epp.ui.activity.work.component.DaggerAddCommentComponent;
import com.wwtech.epp.ui.activity.work.contract.AddCommentContract;
import com.wwtech.epp.ui.activity.work.module.AddCommentModule;
import com.wwtech.epp.ui.activity.work.presenter.AddCommentPresenter;
import com.wwtech.epp.ui.adapter.work.CommentContentListAdapter;
import com.wwtech.epp.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.refactor.library.SmoothCheckBox;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/10/12 20:09:45
 */

public class AddCommentActivity extends BaseActivity implements AddCommentContract.View {

    @Inject
    AddCommentPresenter mPresenter;
    @BindView(R.id.switch_check)
    TextView switchCheck;
    @BindView(R.id.content)
    EditText content;
    @BindView(R.id.save_content)
    SmoothCheckBox saveContent;
    @BindView(R.id.cacanl)
    TextView cacanl;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.recycerView)
    RecyclerView recycerView;
    @BindView(R.id.save_ll)
    LinearLayout saveLl;
    private DBManager mDbManager;
    private CommentContentDao mCommentContentDao;
    private List<CommentContent> contentList;
    CommentContentListAdapter commentContentListAdapter;
    private String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_add_comment);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        saveContent.setChecked(true);
        flag = getIntent().getStringExtra("flag");
    }

    @Override
    protected void initData() {
        setTitle(flag);
        mDbManager = DBManager.init(this);
        mCommentContentDao = mDbManager.getDaoSession().getCommentContentDao();
        contentList = mCommentContentDao.queryBuilder().list();
        KLog.i(contentList.size());
        if (contentList == null || contentList.size() == 0) {
            preData();
            return;
        }
        recycerView.setLayoutManager(new LinearLayoutManager(this));
        commentContentListAdapter = new CommentContentListAdapter(contentList);
        commentContentListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                content.setText(commentContentListAdapter.getData().get(i).getContent());
                recycerView.setVisibility(View.GONE);
                content.setVisibility(View.VISIBLE);
                saveLl.setVisibility(View.VISIBLE);
            }
        });
        recycerView.setAdapter(commentContentListAdapter);
    }

    private void preData() {
        if (flag.equals("批示")) {
            CommentContent commentContent1 = new CommentContent("同意。", 0 + "");
            mCommentContentDao.insert(commentContent1);
        } else if (flag.equals("建议")) {
            CommentContent commentContent1 = new CommentContent("拟同意，请总经理审批。", 0 + "");
            mCommentContentDao.insert(commentContent1);
        } else {
            CommentContent commentContent1 = new CommentContent("妥否，请批示。", 0 + "");
            mCommentContentDao.insert(commentContent1);
        }
        contentList = mCommentContentDao.queryBuilder().list();
        recycerView.setLayoutManager(new LinearLayoutManager(this));
        commentContentListAdapter = new CommentContentListAdapter(contentList);
        commentContentListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                content.setText(commentContentListAdapter.getData().get(i).getContent());
                recycerView.setVisibility(View.GONE);
                content.setVisibility(View.VISIBLE);
                saveLl.setVisibility(View.VISIBLE);
            }
        });
        recycerView.setAdapter(commentContentListAdapter);
    }

    @Override
    protected void onDestroy() {
        mDbManager.uninit();
        mCommentContentDao = null;
        super.onDestroy();
    }

    @Override
    protected void setupActivityComponent() {
        DaggerAddCommentComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .addCommentModule(new AddCommentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(AddCommentContract.AddCommentContractPresenter presenter) {
        mPresenter = (AddCommentPresenter) presenter;
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.activity_translate_out_1);
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void addCommentSuccess() {
        EventBus.getDefault().post("addComment");
//        contentList.add(commentContent);
        for (int i = 0; i < contentList.size(); i++) {
            if (content.getText().toString().equals(contentList.get(i).getContent())) {
                finish();
                return;
            }
        }
        if (saveContent.isChecked()) {
            if (contentList.size() < 4) {
                CommentContent commentContent = new CommentContent(content.getText().toString().trim(), contentList.size() + "");
                mCommentContentDao.insert(commentContent);
            } else {
                mCommentContentDao.deleteAll();
                CommentContent commentContent = new CommentContent(content.getText().toString().trim(), "0");
                mCommentContentDao.insert(commentContent);
                for (int i = 0; i < contentList.size() - 1; i++) {
                    CommentContent commentContent1 = new CommentContent(contentList.get(i).getContent(), i + 1 + "");
                    mCommentContentDao.insert(commentContent1);
                }
            }
        }
        finish();
    }

    @OnClick({R.id.switch_check, R.id.cacanl, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.switch_check:
                recycerView.setVisibility(recycerView.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                if (recycerView.getVisibility() == View.VISIBLE) {
                    content.setVisibility(View.GONE);
                    saveLl.setVisibility(View.GONE);
                } else {
                    content.setVisibility(View.VISIBLE);
                    saveLl.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.cacanl:
                onBackPressed();
                break;
            case R.id.confirm:
                if (content.getText().toString().trim().equals("")) {
                    ToastUtil.displayShortToast("请输入内容");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("userId", ContainValue.userLogin.getData().getUserId() + "");
                map.put("content", content.getText().toString().trim());
                map.put("projectId", getIntent().getStringExtra("projectId"));
                mPresenter.addComment(map);
                break;
        }
    }
}